<?php

namespace App\Http\Controllers;

use App\Models\Ad;
use App\Models\InvitationApply;
use App\Models\Message;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;

class HomeController extends BaseController
{

    /**
     * 广告
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-07
     * @param Request $request
     * @return void
     */
    public function ad()
    {
        return success(Ad::where('status', 1)->select('id', 'img', 'type', 'title', 'is_jump', 'href')->whereNull('delete_time')->get());
    }


    /**
     * 在线留言
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-07
     * @param Request $request
     * @return void
     */
    public function leaveMessage(Request $request)
    {

        $rules = [
            'name' => ['required'],
            'phone' => ['required'],
            'email' => ['required'],
        ];

        $message = [
            'name.required' => '请填写您的姓名',
            'phone.required' => '请填写您的手机号码',
            'email.required' => '请填写您的邮箱',
        ];

        $payload = $request->only('name', 'phone', 'email', 'content');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }

        $result = Message::create($payload);
        if ($result) {
            return success();
        } else {
            return fail_message();
        }
    }


    /**
     * 邀约申请
     *
     * @Author hechengwei
     * @email chichiooo@sina.com
     * @DateTime 2024-08-07
     * @param Request $request
     * @return void
     */
    public function invitationApply(Request $request)
    {

        $rules = [
            'company_name' => ['required'],
            'phone' => ['required'],
        ];

        $message = [
            'company_name.required' => '请填写您的机构名称',
            'phone.required' => '请填写您的手机号码',
        ];

        $payload = $request->only('company_name', 'name', 'phone', 'email', 'speaker_id', 'speaker_name');

        $validator = Validator::make($payload, $rules, $message);

        // 验证格式
        if ($validator->fails()) {
            return fail_message($validator->errors()->first());
        }
        $user = user();
        // $user =  $user->load(['auth']);
        if (empty($user->realname) || !isset($user->realname)) {
            $user->realname = $payload['name'];
        }
        if (empty($user->company_name) || !isset($user->company_name)) {
            $user->company_name = $payload['company_name'];
        }
        if (empty($user->phone) || !isset($user->phone)) {
            $user->phone = $payload['phone'];
        }
        if (empty($user->email) || !isset($user->email)) {
            $user->email = $payload['email'];
        }


        $payload['user_id'] = $user->id;
        // 添加用户修改信息后生成邀约申请

        $result = InvitationApply::create($payload);
        if ($result) {
            $user->save();
            return success();
        } else {
            return fail_message();
        }
    }
}
